﻿using Bestcode.MathParser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.pdf;

namespace Tubes4_Metnum.Machine
{
    public class Simpson1per3
    {
        public MathParser parser;
        public double[] dataX;
        public double[] dataY;
        public double[] tempRes;
        public double[] a;
        public double[] b;
        public double[] h;
        public int[] n;
        public int nVar;
        public double result;

        public Simpson1per3()
        {
            parser = new MathParser();
            this.a = new double[2];
            this.b = new double[2];
            this.n = new int[2];
            this.h = new double[2];
        }

        public void setDataX(double[] data)
        {
            this.dataX = data;
        }

        public void setDataY(double[] data)
        {
            this.dataY = data;
        }

        public void setExpression(String exp)
        {
            parser.Expression = exp;
        }

        public double integral_data(double[] dX, double[] dY)
        {
            this.result = 0;
            nVar = 1;
            this.dataX = dX; this.dataY = dY;
            this.n[0] = dataX.Length - 1;
            this.a[0] = dX[0]; this.b[0] = dX[n[0]];
            this.h[0] = (b[0] - a[0]) / n[0];

            double sigma = dataY[0];
            for (int r = 1; r < n[0]; r++)
            {
                if (r % 2 == 1)
                {
                    sigma += 4 * this.dataY[r];
                }
                else
                {
                    sigma += 2 * this.dataY[r];
                }
            }
            sigma += dataY[n[0]];
            result = (result + sigma) * h[0] / 3;
            
            return result;
        }

        public double integral(String exp, double a, double b, int n)     // n harus genap
        {
            //n HARUS GENAP
            //a titik mulai, b titik akhir, n jumlah pembagi, result hasil
            nVar = 1;
            parser.Expression = exp;
            this.result = 0;
            this.dataX = new double[n + 1];
            this.dataY = new double[n + 1];
            this.a[0] = a; this.b[0] = b; this.n[0] = n;
            this.h[0] = (b - a) / n;

            double sigma = 0;
            double x = a;
            //result = f(a) + f(b)
            parser.X = a; this.dataX[0] = a;
            result += (double)parser.Value;
            this.dataY[0] = (double)parser.Value;
            parser.X = b; this.dataX[n] = b;
            result += (double)parser.Value;
            this.dataY[n] = (double)parser.Value;

            for (int r = 1; r < n; r++)
            {
                x += h[0];
                this.dataX[r] = x;
                if (r % 2 == 1)
                {
                    parser.X = x;
                    sigma += 4 * (double)parser.Value;
                }
                else
                {
                    parser.X = x;
                    sigma += 2 * (double)parser.Value;
                }
                this.dataY[r] = (double)parser.Value;
            }
            result = (result + sigma) * h[0] / 3;
            return result;
        }

        public double integral_ganda(double ax, double bx, double ay, double by, int nx, int ny)
        {
            double result = 0;
            double hx = (bx - ax) / nx;
            double hy = (by - ay) / ny;
            dataX = new double[nx + 1];
            dataY = new double[ny + 1];
            tempRes = new double[nx + 1];
            Console.WriteLine("ax = " + ax);
            Console.WriteLine("bx = " + bx);
            Console.WriteLine("nx = " + nx);
            Console.WriteLine("hx = " + hx);
            for (int i = 0; i < dataX.Length; i++)
            {
                dataX[i] = ax + hx * i;
            }
            for (int i = 0; i < dataY.Length; i++)
            {
                dataY[i] = ay + hy * i;
            }

            String ori_exp = parser.Expression;
            for (int i = 0; i < dataX.Length; i++)
            {
                Simpson1per3 temp = new Simpson1per3();
                String exp = ori_exp.Replace("exp", "eksponen");
                exp = exp.Replace("x", "" + dataX[i]);
                exp = exp.Replace("eksponen", "exp");
                exp = exp.Replace("y", "x");
                tempRes[i] = temp.integral(exp, ax, bx, nx);
                Console.WriteLine(exp);
                Console.WriteLine(tempRes[i]);
            }

            for (int i = 0; i < tempRes.Length; i++)
            {
                if (i == 0 || i == tempRes.Length - 1)
                    result += tempRes[i];
                else if (i % 2 == 0)
                    result += 2 * tempRes[i];
                else
                    result += 4 * tempRes[i];
            }
            result = hy * result / 3;

            this.nVar = 2;
            this.a[0] = ax; this.a[1] = ay;
            this.b[0] = bx; this.b[1] = by;
            this.h[0] = hx; this.h[1] = hy;
            this.n[0] = nx; this.n[1] = ny;

            this.result = result;
            Console.WriteLine("result = " + result);
            return result;
        }

        public void saveFile(string filePath)
        {
            Document document = new Document();
            try
            {
                string ext = filePath.Substring(filePath.Length - 4, 4);
                if (ext.CompareTo(".pdf") != 0) filePath += ".pdf";
                PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
                document.Open();

                document.Add(new Paragraph("Metode Simpson 1/3"));

                PdfPTable ptable;
                PdfPCell pcell;
                if (this.nVar == 1)
                {
                    document.Add(new Paragraph("a = " + this.a[0] + "; b = " + this.b[0] + "; h = " + this.h[0] + "; n = " + this.n[0]));
                    ptable = new PdfPTable(2);
                    iTextSharp.text.Font arial7 = FontFactory.GetFont("ARIAL", 7);
                    pcell = new PdfPCell(new Phrase(new Chunk("X", arial7)));
                    ptable.AddCell(pcell);
                    pcell = new PdfPCell(new Phrase(new Chunk("Y", arial7)));
                    ptable.AddCell(pcell);
                    for (int rows = 0; rows < this.dataX.Length; rows++)
                    {
                        pcell = new PdfPCell(new Phrase(new Chunk(this.dataX[rows].ToString(), arial7)));
                        ptable.AddCell(pcell);
                        pcell = new PdfPCell(new Phrase(new Chunk(this.dataY[rows].ToString(), arial7)));
                        ptable.AddCell(pcell);
                    }
                }
                else
                {
                    document.Add(new Paragraph("ax = " + this.a[0] + "; bx = " + this.b[0] + "; hx = " + this.h[0] + "; nx = " + this.n[0]));
                    document.Add(new Paragraph("ay = " + this.a[1] + "; by = " + this.b[1] + "; hy = " + this.h[1] + "; ny = " + this.n[1]));
                    ptable = new PdfPTable(3);
                    iTextSharp.text.Font arial7 = FontFactory.GetFont("ARIAL", 7);
                    pcell = new PdfPCell(new Phrase(new Chunk("X", arial7)));
                    ptable.AddCell(pcell);
                    pcell = new PdfPCell(new Phrase(new Chunk("Y", arial7)));
                    ptable.AddCell(pcell);
                    pcell = new PdfPCell(new Phrase(new Chunk("integral arah Y", arial7)));
                    ptable.AddCell(pcell);
                    for (int rows = 0; rows < this.dataX.Length; rows++)
                    {
                        pcell = new PdfPCell(new Phrase(new Chunk(this.dataX[rows].ToString(), arial7)));
                        ptable.AddCell(pcell);
                        pcell = new PdfPCell(new Phrase(new Chunk(this.dataY[rows].ToString(), arial7)));
                        ptable.AddCell(pcell);
                        pcell = new PdfPCell(new Phrase(new Chunk(this.tempRes[rows].ToString(), arial7)));
                        ptable.AddCell(pcell);
                    }
                }
                document.Add(new Paragraph("\n"));
                document.Add(ptable);
                document.Add(new Paragraph("Hasil integral: " + this.result));

            }
            catch (DocumentException de)
            {

            }
            catch (IOException ioe)
            {

            }
            document.Close();
        }

    }
}
